
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import vo.CategoriaVO;

public class CategoriaDAO {
    public static List<CategoriaVO> getCategorias() {

        Connection con = ConnectionFactory.getConnection();

        List<CategoriaVO> lista = new ArrayList<>();

        try {

            String query = "SELECT * FROM Categoria";
            query += " order by nome";
            PreparedStatement stmt = con.prepareStatement(query);
            ResultSet res = stmt.executeQuery();

            while (res.next()) {
                String nome = res.getString(1);
                float tarifa = res.getFloat(2);
               
                
                lista.add(new CategoriaVO(nome,tarifa));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lista;
    }
    public static void alteraCategoria(CategoriaVO categoria)
    {
        Connection con= ConnectionFactory.getConnection();
        try{
            String query= "Update Categoria set tarifa=? where nome= ?";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setFloat(1, categoria.getTarifa());
            stmt.setString(2, categoria.getNome());
            
            stmt.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public static Float getTarifaPelaCategoria(String Nome){
        
        float def=0;
        
        Connection con = ConnectionFactory.getConnection();
        
        try{
            String query="Select * from Categoria where nome= ?";
            PreparedStatement stmt=con.prepareStatement(query);
            stmt.setString(1,Nome);
            
          
            ResultSet res = stmt.executeQuery();
            
            while(res.next())
            {
               float   tarifa = res.getFloat(2);
               return tarifa;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        
       return def;
    }
    
    public static void deleteCategoriaPeloNome(CategoriaVO categoria){
        
        Connection con= ConnectionFactory.getConnection();
        try{
            String query= "DELETE from Categoria where nome = ?";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setString(1, categoria.getNome());
            stmt.executeUpdate();
            
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    public static  boolean categoriaExiste(String nome)
    {

        Connection con = ConnectionFactory.getConnection();
        String nomeNovo;
        try{
            String query = "Select * from Categoria where nome= ?";
            PreparedStatement stmt= con.prepareStatement(query);
            stmt.setString(1,nome);

            ResultSet res = stmt.executeQuery();
            while(res.next()){
            nomeNovo = res.getString(1);
            
            if(nomeNovo.equals(nome))
                return true;
            }

        }catch(Exception e){
            e.printStackTrace();
        }
        
        
       return false;       
    }

    public static void insertCategoria(CategoriaVO categoria){
    
        Connection con = ConnectionFactory.getConnection();
        
        try {

            String query = "INSERT INTO Categoria (nome,tarifa) VALUES(?,?)";
            PreparedStatement stmt = con.prepareStatement(query);
            
            stmt.setString(1,categoria.getNome());
            stmt.setFloat(2, categoria.getTarifa());
            stmt.executeUpdate();

           
        } catch (Exception e) {
            e.printStackTrace();
        }
  
    }

    public static List<CategoriaVO> getCategoriaPeloNome(String nome) {
        Connection con = ConnectionFactory.getConnection();
        
        List<CategoriaVO> lista =new ArrayList<>();
        
        try{
            String query = "SELECT * FROM Categoria WHERE nome = ?";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setString(1, nome);
            ResultSet res = stmt.executeQuery();

            while (res.next()) {
                String Nome= res.getString(1);
                Float tarifa = res.getFloat(2);

             lista.add(new CategoriaVO(Nome,tarifa));}
                
        }catch(Exception e){
           e.printStackTrace();
        }
        return lista;
        
    }
           
            
      
}
